package problems;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import lib.MathLib;

public class Euler005 extends AbstractEuler {

	@Override
	public Number calculate() {
		Map<Integer, Integer> requiredFrequencies = new HashMap<Integer, Integer>();

		for (int i = 20; i > 1; i--) {
			List<Integer> factors = MathLib.factorize(i);
			for (Integer factor : factors) {
				int numRequired = Collections.frequency(factors, factor);
				if (requiredFrequencies.get(factor) == null || requiredFrequencies.get(factor) < numRequired) {
					requiredFrequencies.put(factor, numRequired);
				}
			}
		}

		int product = 1;
		
		for (Entry<Integer, Integer> e : requiredFrequencies.entrySet()) {
			product *= Math.pow(e.getKey(), e.getValue());
		}
		
		return product;
	}

	@Override
	protected Number getCorrectAnswer() {
		return 232792560;
	}

}
